package com.example.mywanandroid.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mywanandroid.BR;
import com.example.mywanandroid.R;
import com.example.mywanandroid.bean.responsebean.ArticleBean;
import com.example.mywanandroid.bean.responsebean.NavigationBean;
import com.example.mywanandroid.databinding.ItemNavigationLayoutBinding;
import com.example.mywanandroid.databinding.NaviFragmentBinding;
import com.example.mywanandroid.ui.bottom.navi.NavigationFragment;
import com.example.mywanandroid.ui.bottom.system.SystemViewModel;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.List;

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.SystemViewHolder> {

    private List<NavigationBean>navigationListBean;

    public NavigationAdapter(List<NavigationBean> list) {
        this.navigationListBean = list;
    }

    @NonNull
    @Override
    public SystemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding dataBinding = DataBindingUtil.
                inflate(LayoutInflater.from(parent.getContext()), R.layout.item_navigation_layout, parent, false);
        return new SystemViewHolder(dataBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull SystemViewHolder holder, int position) {
        ItemNavigationLayoutBinding binding = (ItemNavigationLayoutBinding) holder.binding;
        NavigationBean bean = navigationListBean.get(position);
        binding.setNavigationBean(bean);
        showChildView(binding.getRoot().findViewById(R.id.children_view),bean.getArticles(),holder);
        binding.executePendingBindings();
    }

    private void showChildView(RecyclerView recyclerView, List<ArticleBean> articles, SystemViewHolder holder) {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(holder.binding.getRoot().getContext());
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        recyclerView.setLayoutManager(manager);

        CommonAdapter<ArticleBean> commonAdapter = new CommonAdapter<ArticleBean>(articles, R.layout.item_navigation_tag_layout, BR.navigationChildren) {
            @Override
            public void addListener(View root, ArticleBean itemData, int position) {
                root.findViewById(R.id.tv_tag).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        addChildrenListener(root, itemData, position);
                    }
                });
            }
        };
        recyclerView.setAdapter(commonAdapter);
    }

    public void addChildrenListener(View root, ArticleBean itemData, int position) {

    }

    @Override
    public int getItemCount() {
        return navigationListBean !=null ? navigationListBean.size() : 0;
    }

    public void onItemDataChange(List<NavigationBean> item){
        this.navigationListBean = item;
        notifyDataSetChanged();
    }

    class SystemViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding;

        SystemViewHolder(@NonNull ViewDataBinding itemBinding) {
            super(itemBinding.getRoot());
            this.binding = itemBinding;
        }
    }
}
